#!/data/adb/modules/shadow_screenshots/busybox/bin/sh
echo '<?xml version="1.0" encoding="utf-8"?>'
CODDING="https://ak47biubiubiu.coding.net/p/screenshots_shell/d/screenshots_shell/git/raw/master"
MODPATH="/data/adb/modules/shadow_screenshots"
[[ -d ${MODPATH} ]] || MODPATH="/data/adb/lite_modules/shadow_screenshots"
PROP="${MODPATH}/module.prop"
alias convert="${MODPATH}/busybox/bin/magick convert"
export LD_LIBRARY_PATH=${MODPATH}/busybox/lib:${LD_LIBRARY_PATH}
source ${PROP}
PNG_TMP_PATH="${START_DIR}/png"
PNG_MOD_PATH="${MODPATH}/shell"
chmod -R +r ${PNG_MOD_PATH}
MD5LOG=${TEMP_DIR}/md5.log
rm -f ${MD5LOG}
ls ${PNG_MOD_PATH} | while read line
do
	md5sum ${PNG_MOD_PATH}/${line} >> ${MD5LOG}
done
[[ -d ${PNG_TMP_PATH} ]] || mkdir -p ${PNG_TMP_PATH}
function build()
{
	PNG="${PNG_MOD_PATH}/${1}.png"
	ICON="${PNG_TMP_PATH}/${1}.png"
	if [[ -f "${PNG}" ]]; then
		SHELL="hidden"
		MD5="md5"
	else
		SHELL="bg-task"
		MD5="${2}"
	fi
	if [[ ! -f "${ICON}" ]]; then
		if [[ -f "${PNG}" ]]; then
			convert -resize 100x100^ "${PNG}" "${ICON}" 2>/dev/null
		else
			curl -o ${ICON} ${CODDING}/${1}.png
			file ${ICON} | egrep "png|PNG|jpg|JPG|jpeg|JPEG" > /dev/null
			[[ $? -ne 0 ]] && rm -f ${ICON}
		fi
	fi
	cat <<-EOF
		<group>
			<action shell="${SHELL}" icon="${ICON}" reload="true">
				<title>$1</title>
				<set>
					if [[ -f ${PNG_MOD_PATH}/${1}.png ]]; then
						sed -i 's/^shell_name=.*/shell_name=&#34;${1}&#34;/g' ${PROP}
					else
						TMP=/dev/shell_tmp
						mkdir -p \${TMP}
						FILE=\${TMP}/${1}.zip
						curl -o \${FILE} ${CODDING}/${1}.zip
						if [[ \$(md5sum \${FILE} | cut -d ' ' -f 1) == ${MD5} ]]; then
							unzip -o \${FILE} -d \${TMP}
							cp -f \${TMP}/${1}.*g ${PNG_MOD_PATH}
							if [[ \$? -eq 0 ]]; then
								echo '导入完成'
								sed -i 's/^shell_name=.*/shell_name=&#34;${1}&#34;/g' ${PROP}
							else
								echo '导入失败，请重新下载'
							fi
						else
							echo '下载丢包，请重新下载'
						fi
						rm -rf \${TMP}
					fi
				</set>
			</action>
		</group>
	EOF
}

function new_build()
{
	if [[ ! -f "${PNG_MOD_PATH}/${1}.png" ]]; then
		grep "$3" ${MD5LOG} > /dev/null
		if [[ $? -ne 0 ]]; then
			build $@
		fi
	fi
}

echo '<text><slice color="#ff0000">管理:</slice></text>'
cat <<-EOF
	<group>
		<action reload="true">
			<title>输入分享码获取模板</title>
			<param
				name="switch"
				label="导入成功后直接切换"
				type="switch"
				value="1"/>
			<param
				name="code"
				type="text"
				maxlength="6"
				placeholder="输入分享码获取模板"/>
			<set>
				TMP=/dev/shell_tmp
				FILE=\${TMP}/shell.zip
				mkdir -p \${TMP}
				echo '-下载模板'
				curl -o \${FILE} -L https://dlj.li/\${code}
				if [[ \$? -eq 0 ]]; then
					echo '-下载完成，准备解压'
					unzip -o \${FILE} -d \${TMP}
					if [[ \$? -eq 0 ]]; then
						if [[ -n &#34;\$(ls \${TMP}/*.png)&#34; ]] &#38;&#38; [[ -n &#34;\$(ls \${TMP}/*.cfg)&#34; ]]; then
							echo '-解压完成，开始导入'
							shell_name=\$(ls \${TMP}/*.png | sed 's/.*\/\(.*\).png/\1/g')
							cp -f \${TMP}/\${shell_name}.png ${PNG_MOD_PATH}
							cp -f \${TMP}/\${shell_name}.cfg ${PNG_MOD_PATH}
							echo \${code} > ${TEMP_DIR}/\${shell_name}.share
							echo '-导入成功'
							if [[ \${switch} == 1 ]]; then
								sed -i 's/^shell_name=.*/shell_name=&#34;'\${shell_name}'&#34;/g' ${PROP}
								echo '-切换成功'
							fi
						else
							echo '-模板结构不正确'
						fi
					else
						echo '-解压出错，可能的原因:'
						echo '	① 下载不完整，建议重试'
						echo '	② 分享码不正确或已过期'
						echo '	③ 模板本身可能有问题'
						echo '	④ ...'
					fi
				else
					echo '-下载出错，请检查网络连接并重试'
				fi
				rm -rf \${TMP}
			</set>
		</action>
		<action reload="true">
			<title>导入自制模板</title>
			<param
				name="SWITCH"
				label="导入成功后直接切换"
				type="switch"
				value="1"/>
			<param
				name="FILE"
				title="模板图片路径"
				type="file"
				suffix="png"
				editable="true"/>
EOF
function axis()
{
	cat <<-EOF
			<param
				name="$1"
				label="$2"
				type="int"
				min="0"/>
	EOF
}
axis left_top_x 左上角X轴
axis left_top_y 左上角Y轴
axis left_bottom_x 左下角X轴
axis left_bottom_y 左下角Y轴
axis right_top_x 右上角X轴
axis right_top_y 右上角Y轴
axis right_bottom_x 右下角X轴
axis right_bottom_y 右下角Y轴
cat <<-EOF
			<set>
				alias identify=''${MODPATH}'/busybox/bin/magick identify'
				export LD_LIBRARY_PATH=${MODPATH}/busybox/lib:\${LD_LIBRARY_PATH}
				WH=\$(identify &#34;\${FILE}&#34; 2>/dev/null | egrep -o ' [0-9]+x[0-9]+ ' | tr -d ' ')
				W=\${WH%%x*}
				H=\${WH##*x}
				if [[ \${left_top_x} -le \${W} ]] &#38;&#38; \
				[[ \${left_bottom_x} -le \${W} ]] &#38;&#38; \
				[[ \${right_top_x} -le \${W} ]] &#38;&#38; \
				[[ \${right_bottom_x} -le \${W} ]] &#38;&#38; \
				[[ \${left_top_y} -le \${H} ]] &#38;&#38; \
				[[ \${left_bottom_y} -le \${H} ]] &#38;&#38; \
				[[ \${right_top_y} -le \${H} ]] &#38;&#38; \
				[[ \${right_bottom_y} -le \${H} ]]; then
					echo '-符合要求，开始导入'
					cp -f &#34;\${FILE}&#34; ${PNG_MOD_PATH}
					shell_name=\$(echo \${FILE##*/} | sed 's/....$//g')
					echo -e &#34;left_top_x=\${left_top_x}\nleft_top_y=\${left_top_y}\n\nleft_bottom_x=\${left_bottom_x}\nleft_bottom_y=\${left_bottom_y}\n\nright_top_x=\${right_top_x}\nright_top_y=\${right_top_y}\n\nright_bottom_x=\${right_bottom_x}\nright_bottom_y=\${right_bottom_y}\n\ntemplate_width=\${W}\ntemplate_height=\${H}&#34; > ${PNG_MOD_PATH}/\${shell_name}.cfg
					if [[ \$? -eq 0 ]]; then
						echo '-导入成功'
						if [[ \${SWITCH} == 1 ]]; then
							sed -i 's/^shell_name=.*/shell_name=&#34;'\${shell_name}'&#34;/g' ${PROP}
							echo '-切换成功'
						fi
					else
						echo '-导入失败，禁止出现空格'
					fi
				else
					echo '-坐标瞄点不可超出图片分辨率'\${WH}''
				fi
			</set>
		</action>
		<picker shell="hidden" reload="true" multiple="true" options-sh="ls ${PNG_MOD_PATH}/*.png | sed 's/.*\/\(.*\).png/\1/g'">
			<title>删除模板</title>
			<set>
				echo &#34;\${state}&#34; | while read line
				do
					if [[ \${line} != ${shell_name} ]]; then
						rm -f ${PNG_MOD_PATH}/\${line}.png ${PNG_MOD_PATH}/\${line}.cfg
						if [[ -f ${TEMP_DIR}/\${line}.share ]]; then
							rm -f ${TEMP_DIR}/\${line}.share
						fi
					else
						echo '不允许删除使用中的模板'
					fi
				done
			</set>
		</picker>
		<action shell="hidden" reload="true">
			<title>重命名模板</title>
			<param
				name="STATE"
				title="选择模板"
				value="${shell_name}"
				options-sh="ls ${PNG_MOD_PATH}/*.png | sed 's/.*\/\(.*\).png/\1/g'"/>
			<param
				name="TEXT"
				title="输入新的模板名字"
				placeholder="禁止出现空格 尽量不要特殊字符"/>
			<set>
				if [[ ! -f ${PNG_MOD_PATH}/\${TEXT}.png ]]; then
					mv -f ${PNG_MOD_PATH}/\${STATE}.png ${PNG_MOD_PATH}/\${TEXT}.png
					mv -f ${PNG_MOD_PATH}/\${STATE}.cfg ${PNG_MOD_PATH}/\${TEXT}.cfg
					if [[ -f ${PNG_TMP_PATH}/\${STATE}.png ]]; then
						mv -f ${PNG_TMP_PATH}/\${STATE}.png ${PNG_TMP_PATH}/\${TEXT}.png
					fi
					if [[ ${shell_name} == \${STATE} ]]; then
						sed -i 's/^shell_name=.*/shell_name=&#34;'\${TEXT}'&#34;/g' ${PROP}
					fi
					if [[ -f ${TEMP_DIR}/\${STATE}.share ]]; then
						mv -f ${TEMP_DIR}/\${STATE}.share ${TEMP_DIR}/\${TEXT}.share
					fi
				fi
			</set>
		</action>
		<action reload="true">
			<title>模板重构</title>
			<param
				name="NAME"
				title="选择模板"
				value="${shell_name}"
				options-sh="ls ${PNG_MOD_PATH}/*.png | grep -v '重构.png' | sed 's/.*\/\(.*\).png/\1/g'"/>
			<param
				title="模板缩放系数 %"
				name="RATIO"
				type="seekbar"
				min="10"
				max="400"
				value="100"/>
			<param
				title="可以为模板自定义一个背景哦"
				name="BG"
				type="file"
				editable="true"/>
			<param
				title="背景缩放系数 %"
				name="BGRATIO"
				type="seekbar"
				min="10"
				max="400"
				value="100"/>
			<param
				title="背景高斯模糊"
				name="BLUR"
				type="seekbar"
				min="0"
				max="100"
				value="0"/>
			<set>
				alias identify=''${MODPATH}'/busybox/bin/magick identify'
				alias convert=''${MODPATH}'/busybox/bin/magick convert'
				export LD_LIBRARY_PATH=${MODPATH}/busybox/lib:\${LD_LIBRARY_PATH}
				file='/dev/make_shell'
				mkdir -p \${file}
				source ${PNG_MOD_PATH}/\${NAME}.cfg
				left_top_x=\$(( \${left_top_x} * \${RATIO} / 100 ))
				left_top_y=\$(( \${left_top_y} * \${RATIO} / 100 ))
				left_bottom_x=\$(( \${left_bottom_x} * \${RATIO} / 100 ))
				left_bottom_y=\$(( \${left_bottom_y} * \${RATIO} / 100 ))
				right_top_x=\$(( \${right_top_x} * \${RATIO} / 100 ))
				right_top_y=\$(( \${right_top_y} * \${RATIO} / 100 ))
				right_bottom_x=\$(( \${right_bottom_x} * \${RATIO} / 100 ))
				right_bottom_y=\$(( \${right_bottom_y} * \${RATIO} / 100 ))
				template_width=\$(( \${template_width} * \${RATIO} / 100 ))
				template_height=\$(( \${template_height} * \${RATIO} / 100 ))
				echo -e &#34;left_top_x=\${left_top_x}\nleft_top_y=\${left_top_y}\n\nleft_bottom_x=\${left_bottom_x}\nleft_bottom_y=\${left_bottom_y}\n\nright_top_x=\${right_top_x}\nright_top_y=\${right_top_y}\n\nright_bottom_x=\${right_bottom_x}\nright_bottom_y=\${right_bottom_y}\n\ntemplate_width=\${template_width}\ntemplate_height=\${template_height}&#34; > \${file}/\${NAME}.cfg
				echo '重构后分辨率'\${template_width}'x'\${template_height}''
				convert -resize \${template_width}x\${template_height}! ${PNG_MOD_PATH}/\${NAME}.png \${file}/\${NAME}.png 2>/dev/null
				if [[ -f &#34;\${BG}&#34; ]]; then
					convert -blur 0x\${BLUR} &#34;\${BG}&#34; -resize \${BGRATIO}% \${file}/bg.png 2>/dev/null
					BG=\${file}/bg.png
					BGWH=\$(identify &#34;\${BG}&#34; 2>/dev/null | egrep -o ' [0-9]+x[0-9]+ ' | tr -d ' ')
					BGW=\${BGWH%%x*}
					BGH=\${BGWH##*x}
					echo '背景分辨率'\${BGW}'x'\${BGH}''
					if [[ \${template_width} -le \${BGW} ]] &#38;&#38; \
					[[ \${template_height} -le \${BGH} ]]; then
						echo '背景图片符合要求，开始导入'
						coordinate="\${left_top_x},\${left_top_y} \${left_bottom_x},\${left_bottom_y} \${right_bottom_x},\${right_bottom_y} \${right_top_x},\${right_top_y}"
						convert &#34;\${BG}&#34; -alpha set \( \${file}/\${NAME}.png -alpha transparent -fill white -draw "path 'M \$coordinate'" \) -compose xor -gravity center -composite \${file}/\${NAME}.png -compose over -gravity center -composite \${file}/\${NAME}.png 2>/dev/null
						W=\$(( ( \${BGW} - \${template_width} ) / 2 ))
						H=\$(( ( \${BGH} - \${template_height} ) / 2 ))
						left_top_x=\$(( \${left_top_x} + \${W} ))
						left_top_y=\$(( \${left_top_y} + \${H} ))
						left_bottom_x=\$(( \${left_bottom_x} + \${W} ))
						left_bottom_y=\$(( \${left_bottom_y} + \${H} ))
						right_top_x=\$(( \${right_top_x} + \${W} ))
						right_top_y=\$(( \${right_top_y} + \${H} ))
						right_bottom_x=\$(( \${right_bottom_x} + \${W} ))
						right_bottom_y=\$(( \${right_bottom_y} + \${H} ))
						echo -e &#34;left_top_x=\${left_top_x}\nleft_top_y=\${left_top_y}\n\nleft_bottom_x=\${left_bottom_x}\nleft_bottom_y=\${left_bottom_y}\n\nright_top_x=\${right_top_x}\nright_top_y=\${right_top_y}\n\nright_bottom_x=\${right_bottom_x}\nright_bottom_y=\${right_bottom_y}\n\ntemplate_width=\${template_width}\ntemplate_height=\${template_height}&#34; > \${file}/\${NAME}.cfg
					else
						echo '背景图片尺寸过小'
					fi
				fi
				if [[ -f &#34;\${file}/\${NAME}.cfg&#34; ]] &#38;&#38; \
				[[ -f &#34;\${file}/\${NAME}.png&#34; ]]; then
					cp -f \${file}/\${NAME}.cfg ${PNG_MOD_PATH}/\${NAME}重构.cfg
					cp -f \${file}/\${NAME}.png ${PNG_MOD_PATH}/\${NAME}重构.png
					sed -i 's/^shell_name=.*/shell_name=&#34;'\${NAME}重构'&#34;/g' ${PROP}
					rm -rf \${file}
					echo '导入成功'
				fi
			</set>
		</action>
	</group>
EOF

curl -o ${PAGE}/make_gui_pio.sh -sL ${CODDING}/.make_gui_pio.sh
if [[ -f "${PNG_MOD_PATH}/${shell_name}.cfg" ]]; then
	source ${PNG_MOD_PATH}/${shell_name}.cfg
	W=$(echo ${template_width} | egrep -o "[0-9]+")
	H=$(echo ${template_height} | egrep -o "[0-9]+")
	MD5=`md5sum "${PNG_MOD_PATH}/${shell_name}.png" | cut -d ' ' -f 1`
	grep ${MD5} ${PAGE}/make_gui_pio.sh > /dev/null
	if [[ $? -eq 0 ]]; then
		VISIBLE=0
		DESC="-分辨率: ${W}x${H}&#x000A;-仓库模板不允许生成分享码哦"
	else
		SHARE="${TEMP_DIR}/${shell_name}.share"
		if [[ -f "${SHARE}" ]]; then
			VISIBLE=0
			DESC="-分辨率: ${W}x${H}&#x000A;-分享码: `cat ${SHARE} | head -n 1`"
		else
			VISIBLE=1
			DESC="-分辨率: ${W}x${H}&#x000A;-点击获取分享码"
		fi
	fi
else
	VISIBLE=0
	DESC="-配置丢失，请重新导入模板"
fi

ICON="${PNG_TMP_PATH}/${shell_name}.png"
if [[ ! -f "${ICON}" ]]; then
	convert -resize 100x100^ "${PNG_MOD_PATH}/${shell_name}.png" "${ICON}" 2>/dev/null
fi
echo '<text><slice color="#ff0000">使用中:</slice></text>'
if [[ ${VISIBLE} -eq 0 ]]; then
	cat <<-EOF
		<group>
			<action icon="${ICON}" shell="hidden">
				<title>${shell_name}</title>
				<desc>${DESC}</desc>
			</action>
		</group>
	EOF
else
	cat <<-EOF
		<group>
			<action icon="${ICON}" reload="true">
				<title>${shell_name}</title>
				<desc>${DESC}</desc>
				<set>
					file=/dev/shadow_mod_share
					mkdir -p \${file}
					cd \${file}
					cp -f ${PNG_MOD_PATH}/${shell_name}.cfg \${file}
					cp -f ${PNG_MOD_PATH}/${shell_name}.png \${file}
					echo '-打包配置'
					zip -q -9 ${shell_name}.zip ${shell_name}.cfg ${shell_name}.png
					if [[ \$? -eq 0 ]]; then
						echo '-上传配置'
						url=\$(curl -F &#34;name=@\${file}/${shell_name}.zip&#34; https://img.vim-cn.com)
						if [[ \$? -eq 0 ]]; then
							echo '-生成分享码'
							api=https://dlj.li/api.php?url=\${url}
							json=\$(curl -sL \${api})
							#echo \${json}
							eval \$(echo \${json} | tr -d '{}&#34;' | tr ':,' '=\n')
							if [[ \$msg == success ]]; then
								echo '----------------------------------------'
								echo '----------------------------------------'
								echo -你的分享码: \${code}
								echo \${code} > ${TEMP_DIR}/${shell_name}.share
							else
								echo '-api异常，暂无法生成分享码'
							fi
						else
							echo '-上传失败，请检查网络连接'
						fi
					else
						echo '-打包失败'
					fi
					rm -rf \${file}
				</set>
			</action>
		</group>
	EOF
fi

echo '<text><slice color="#ff0000">已安装:</slice></text>'
ls ${PNG_MOD_PATH}/*.png | sed 's/.*\/\(.*\).png/\1/g' | while read line
do
	[[ ${line} == ${shell_name} ]] || build ${line}
done

source ${PAGE}/make_gui_pio.sh